import java.util.ArrayList;

import processing.core.PApplet;
import processing.core.PFont;

public class LayoutGenerator extends PApplet{
	Layout layout;
	ArrayList rects;
	int index;
	Edge e1, e2;
	PFont font;
	public void setup(){
		size(800, 800);
		frameRate(20);
		smooth();
		rects = new ArrayList();
		font = loadFont("LiGothicMed-36.vlw");
		layout = new Layout(font);
		e1 = new Edge(25, 25, 100, Edge.HORIZONTAL, Edge.HIGH);
		e2 = new Edge(25, 25, 125, Edge.HORIZONTAL, Edge.LOW);
	}
/*
	public void draw(){
		background(20);
		translate(400, 400);
		stroke(255, 0, 0, 125);
		fill(255, 0, 0, 125);
		e1.draw(this);
		stroke(0, 255, 0, 125);
		fill(0, 255, 0, 125);
		e2.draw(this);
	}
	*/
	
	public void draw(){
		background(250);
		translate(400, 400);
		layout.draw(this);
	}
	
	
	public void keyPressed(){
		if(keyCode == LEFT || keyCode == RIGHT || keyCode == UP || keyCode == DOWN){
			println("pressed");

			int side = Edge.LEFT;
			if(keyCode == RIGHT) side = Edge.RIGHT;
			if(keyCode == UP) side = Edge.TOP;
			if(keyCode == DOWN) side = Edge.BTM;
			float w = (int)Math.floor(random(1, 7)) * 20;
			float h = (int)Math.floor(random(1, 6)) * 20;
			layout.add(new RectNode(w, h), side);

			//System.out.println(layout.listToString(layout.edge));
		}
		if(keyCode == this.ENTER){
			layout = new Layout(font);
			
		}
		
		if(key == 'p'){
			System.out.println(layout.lastAnchor.result.toString());
		}
		
		if(key == 't'){
			tryNextBest();
		}
	}
	int best = 0;
	public void tryNextBest(){
		layout.undo();
		Anchor next = (Anchor)layout.prevAnchors.get(best);
		next.placeRect(layout.lastRect, next.getSpace(next.prefSpace));
		layout.borg.assimilate(layout.lastRect);
		layout.lastAnchor = next;
		best++;
		layout.oldBorg = layout.borg;
		if(best >= layout.prevAnchors.size()) best = 0;
	}
	
	/*
	public void keyPressed(){
		if(keyCode == LEFT) e1.pos.x -= 25;
		if(keyCode == RIGHT) e1.pos.x += 25;
		if(keyCode == UP) e1.pos.y -= 25;
		if(keyCode == DOWN) e1.pos.y += 25;
		if(key == ',') e1.l -= 25;
		if(key == '.') e1.l += 25;
		if(e1.equalPerp(e2)){
			System.out.println("Equal perp");
			System.out.println(e1.getOverlapString(e1.overlap(e2)));
				
		}
	}
	*/
	
}
